
let App = getApp();
var that;

Page({
	data:{
		  region: ['广东省', '广州市', '海珠区'],
		  customItem: '全部',
		  name:'',
		  mobile:'',
		  detail:'',
		  address_id:'',
		  // province_name:'',
		  // city_name:'',
		  region:'',
		  isEdit:false,
    isSwitchAddress: false,	
    coupon_code: '',
    coupon_id: ''	  
	},
	name(e){
		that.setData({
			name: e.detail.value
		})
	},
	mobile(e){
		that.setData({
			mobile: e.detail.value
		})
	},
	detail(e){
		that.setData({
			detail: e.detail.value
		})
	},
	onLoad: function (options) {
		console.log(options)
        that = this;
    if (options.isSwitchAddress=='true'){
      that.setData({
        isSwitchAddress: options.isSwitchAddress,
        coupon_code: options.coupon_code,
        coupon_id: options.coupon_id

      })
    }
   
        // 注册通知
        // WxNotificationCenter.addNotification("poiSelectedNotification",that.getAddress,that);
        // 属于编辑状态
        if (options.objectId) {
           
            that.setData({
                isEdit: true,
            
				name:options.name,
				mobile:options.phone,
				province_name:options.province_name,
				city_name:options.city_name,
				region:[options.province_name,options.city_name,options.region],
				detail:options.text,
				address_id:options.objectId
				
            });
            wx.setNavigationBarTitle({
                title: '编辑地址'
            })
        } else {
            wx.setNavigationBarTitle({
                title: '添加地址'
            })
        }
	},
	//点击确定按钮
	 bindRegionChange: function (e) {
	    console.log('picker发送选择改变，携带值为', e.detail.value)
	    this.setData({
	      region: e.detail.value
	    })
	  },

   //添加地址
    add: function (e) {
		//编辑
		that = this
      console.log(that.data.isSwitchAddress)
		if(that.data.isEdit==true){
			wx.request({
				url: App.url.requestUrl + 'Adress/edit',
				method: 'POST',
				 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
				dataType:'json',
				data:{
					address_id:that.data.address_id,
					name:that.data.name,
					phone:that.data.mobile,
					province_name:that.data.region[0],
					city_name:that.data.region[1],
					region_name:that.data.region[2],
					detail:that.data.detail
					
				},
				success: function (res) {
				  if(res.data.ret == 200){
					  
				    wx.showToast({
				      title: '修改成功',
				      icon: 'success',
				      duration: 2000
				    })
				    // 跳转到订单展示界面
				    setTimeout(function(){
				      wx.navigateTo({
                url: '../list/list?isSwitchAddress=' + that.data.isSwitchAddress + '&coupon_code=' + that.data.coupon_code + '&coupon_id=' + that.data.coupon_id,
				      })
				    },2500)
						  
					 }
				}
			})
		}else{
			//添加
        if (that.data.name == '') {
            wx.showModal({
                title: '请填写收件人姓名',
                showCancel: false
            });
            return;
        }

        if(!(/^1[34578]\d{9}$/.test(that.data.mobile))){ 
            wx.showModal({
                title: '请填写正确手机号码',
                showCancel: false
            });
            return;
        }

        if (that.data.detail == '') {
            wx.showModal({
                title: '请填写详细地址',
                showCancel: false
            });
            return;
        }
        
		
		wx.request({
			url: App.url.requestUrl + 'Adress/add',
			method: 'POST',
			 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
			dataType:'json',
			data:{
				name:that.data.name,
				phone:that.data.mobile,
				province_name:that.data.region[0],
				city_name:that.data.region[1],
				region_name:that.data.region[2],
				detail:that.data.detail
				
			},
			success: function (res) {
			  if(res.data.ret == 200){
				  
			    wx.showToast({
			      title: '添加成功',
			      icon: 'success',
			      duration: 2000
			    })
			    // 跳转到订单展示界面
			    setTimeout(function(){
			      wx.navigateTo({
              url: '../list/list?isSwitchAddress=' + that.data.isSwitchAddress + '&coupon_code=' + that.data.coupon_code + '&coupon_id=' + that.data.coupon_id,
			      })
			    },2500)
					  
				 }
			}
		})
		}
        
        // 是否处在编辑状态
        if (that.data.isEdit) {
            // address = that.data.address;
        }
      ;
    },
	
	
  
    delete: function () {
        // 确认删除对话框
        wx.showModal({
            title: '确认删除',
            success: function (res) {
                if (res.confirm) {
                    var address = that.data.address;
                    address.destroy().then(function (result) {
                        wx.showModal({
                            title: '删除成功',
                            showCancel: false,
                            success: function () {
                                wx.navigateBack();
                            }
                        });
                    });
                }
            }
        });
        
    }
})